package com.dbh.company.blockchain;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import java.io.File;
import java.util.List;

/**
 * @author dongbanghui
 * @create 2023/3/6
 */
public class HouseImageUtil {

    public static void downloadImage(String filePath) {
        String str = FileUtil.readString(filePath, CharsetUtil.CHARSET_UTF_8);
        JSONObject jsonObject = JSONUtil.parseObj(str);
        JSONArray jsonArray = JSONUtil.parseArray(jsonObject.get("data"));
        FileUtil.mkdir( FileUtil.getParent(filePath, 1) + "\\image");
        jsonArray.forEach(item -> {
            JSONObject jsonObject2 = JSONUtil.parseObj(item);
            String houseDelCode = Convert.toStr(jsonObject2.get("houseDelCode"));
            FileUtil.mkdir( FileUtil.getParent(filePath, 1) + "\\image\\" + houseDelCode);
            List<String> actualUrlList = (List<String>) Convert.toList(jsonObject2.get("actualUrlList"));
            int count = 0;
            if (CollUtil.isNotEmpty(actualUrlList)) {
                actualUrlList.forEach(imageUrl -> {
                    try {
                        HttpUtil.downloadFile(imageUrl, FileUtil.getParent(filePath, 1) + "\\image\\" + houseDelCode);
                    } catch (Exception e) {
                    }
                });
            }
            List<String> imageUrlList = (List<String>) Convert.toList(jsonObject2.get("imageUrlList"));
            if (CollUtil.isNotEmpty(imageUrlList)) {
                imageUrlList.forEach(imageUrl -> {
                    try {
                        HttpUtil.downloadFile(imageUrl, FileUtil.getParent(filePath, 1) + "\\image\\" + houseDelCode);
                    } catch (Exception e) {
                    }
                });
            }

        });
    }

    public static void main(String[] args) {
        downloadImage("F:\\a.txt");
    }

}
